package com.xs.bookspoststation.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xs.bookspoststation.bean.Book;

import java.util.ArrayList;
import java.util.List;

public class BookBean {
    private Context context;
    private DBHelperBook dbHelperBook;
    public BookBean(Context context){
        this.context=context;
        dbHelperBook=new DBHelperBook(context,"book.db",null,1);
    }

    @SuppressLint("Range")
    public List<Book> findAll(){
        List<Book> books = null;
        SQLiteDatabase db = dbHelperBook.getWritableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query("bookInfo", null, null, null, null, null, null);
            books = new ArrayList<Book>();
            while (cursor.moveToNext()) {
                Book book = new Book();
                //Xxx getXxx(columnIndex):根据字段下标得到对应的值
                //int getColumnIndex():根据字段名得到对应的下标
                //cursor.getLong()：以 long 形式返回所请求列的值。
                //getColumnIndex() 获取给定列名的从零开始的列索引,如果列名不存在返回-1
                //bookid指图书编号，bookname指图书名称，booknumber指数量,bookauthor作者，bookshow介绍
                String bookid = cursor.getString(cursor.getColumnIndex("bookid"));
                book.setBookid(bookid);
                String bookname = cursor.getString(cursor.getColumnIndex("bookname"));
                book.setBookname(bookname);
                String booknumber = cursor.getString(cursor.getColumnIndex("booknumber"));
                book.setBooknumber(booknumber);
                String bookauthor = cursor.getString(cursor.getColumnIndex("bookauthor"));
                book.setBookauthor(bookauthor);
                String bookshow = cursor.getString(cursor.getColumnIndex("bookshow"));
                book.setBookshow(bookshow);
                books.add(book);
            }
            cursor.close();
            db.close();
        }
        return books;
    }
}
